引用
import { useGlobalState, setGlobalState, getGlobalState } from "cl-util";
第一步
任意位置初始化并设置全局状态
setGlobalState("ticker-test", 1);
setGlobalState(Symbol(), {a:1, b: "hello", c: []})
第二步
在用到全局状态的组件中调用自定义Hooks
function MyComponent(){
const [ticker, setTicker] = useGlobalState("ticker-test");
return (
<div>
<button
onClick={()=>{
setTicker(ticker+1);
}}
>
点击更新ticker值
</button>
<p>当前{ticker}</p>
</div>
);
}
其他
只要setGlobalState
被初始化过了,接下来可以在任意地方调用,甚至可以在组件之外调用
setInterval(()=>{
const ticker = getGlobalState("ticker-test");
setGlobalState("ticker-test", ticker + 1);
}, 1000)